#script2303.py - Play Music from List
#Written by Blum and Bresnahan
# 
##########################################
#
##### Import Functions & Variables #######
#
from os import system		#Import from OS module
#
				#Import from PyGame Library
from pygame import display, event, font, init, mixer, time
#
from sys import exit		#Import from System module
#
from pygame.locals import *	#Load PyGame constants
#
mixer.pre_init(44100,-16,2,1024)#Set Mixer Settings
mixer.init()			#Intialize Mixer
#
init()				#Initialize PyGame
#
# Load Music Play List Function ##################
#
def Load_Music ():
#
	SongList=[]		#Initialize SongList to Null
	#
	PlayList=MusicDirectory + '/' + 'playlist.txt'
	PlayList=open(PlayList, 'r')
	#
	for Song in PlayList:	#Load PlayList into SongList
	#
		Song=Song.rstrip('\n') #Strip off newline
		if Song != "":   #Avoid blank lines in PlayList
			Song=MusicDirectory + '/' + Song
			SongList.append(Song)
	PlayList.close()
	#
	return SongList
#
# Play The Music Function #########################
#
def Play_Music (SongList,SongIndexNo):
	mixer.music.load(SongList[SongIndexNo])
	mixer.music.play(0)
	mixer.music.set_endevent(USEREVENT)	#Send event when Music Stops
#
# Display the Song Function #######################
#
def Display_Song (NowPlaying,MusicTitleGraphic,MusicFont,blue,black):
#
	MusicTextGraphic=MusicFont.render(NowPlaying,True,black)
	MusicScreen.fill(blue)
	MusicScreen.blit(MusicTitleGraphic,(50,50))
	MusicScreen.blit(MusicTextGraphic,(100,100))
	display.update()
#
# Gracefully Exit Script Function #################
#
def Graceful_Exit ():
	mixer.music.stop() #Stop any music.
	mixer.quit()	   #Quit mixer
	time.delay(3000)   #Allow things to shutdown
	Command="sudo umount " + MusicDisk
	system(Command)    #Unmount disk
	exit()
#	
# Set up Music Variables ######################
# 
MusicDirectory='/home/pi/music'
MusicDisk='/dev/sda1'
#
# Mount the Music Drive ########################
#
Command="sudo umount " + MusicDisk + " 2>/dev/null"
system(Command)
Command="sudo mount -t vfat " + MusicDisk + " " + MusicDirectory
system(Command)
#
# Queue up the Music #############################
#
SongList=Load_Music()		#Create a Song List from Play list file
#
MaxSongs=len(SongList)- 1	#Get Maximum Songs index number
SongIndexNo = 0 		#Set index number to first song in list
#
Play_Music(SongList,SongIndexNo)
NowPlaying=SongList[SongIndexNo]
SongIndexNo += 1
#
# Set up Display for Event Handling ##############
#
MusicScreen=display.set_mode((0,0))
display.set_caption("Playing Music...")
#
# Set up Display for Now Playing ################
#
black=0,0,0
blue=0,0,255
MusicFont=font.Font(None,60)
MusicTitleGraphic=MusicFont.render("Now Playing...",True,black)
#
# Display first song
Display_Song(NowPlaying,MusicTitleGraphic,MusicFont,blue,black)
#
while True:  #Keep playing the Music ############
	for Event in event.get():
	#
		if Event.type == USEREVENT:   #Play another song
		#
			if SongIndexNo <= MaxSongs:
				Play_Music(SongList,SongIndexNo)
				NowPlaying=SongList[SongIndexNo]
				Display_Song(NowPlaying,MusicTitleGraphic,MusicFont,blue,black)
				# 
				if SongIndexNo == MaxSongs:  	# Loop
					SongIndexNo=0
				else:
					SongIndexNo += 1	# Continue
		#
		if Event.type in (QUIT,KEYDOWN,MOUSEBUTTONDOWN): # Exit
			Graceful_Exit()
#
